class_name CircleBtnView
extends BaseView

#region 属性
@export var slot_btn_scene:PackedScene
@export var desc:Label
@onready var weapon_slot_btns: Control = %WeaponSlotBtns
var _slot_btns:Array[SlotBtn] = []
var _entity:PlayerEntity
var _amount:int
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
func init_view():
	super.init_view()
	_entity = GamePlayerSystem.get_player()
	_amount = _entity.get_entity_data().weapon_max_amount
	_entity.get_entity_data().data_changed.connect(_on_weapon_max_amount_changed)
	for i in range(8):
		var slot_btn:SlotBtn = slot_btn_scene.instantiate()
		weapon_slot_btns.add_child(slot_btn)
		slot_btn.init_slot_btn(self,i)
		slot_btn.visible = i < _amount
		_slot_btns.append(slot_btn)
	update_desc(-1)
func on_enter(args:Dictionary = {}):
	super.on_enter()
	GameManager.translate_game_state(GameEnum.GameState.Pausing)
	update_slot_btns()
func on_pause(args:Dictionary = {}):
	super.on_pause()
func on_resume(args:Dictionary = {}):
	super.on_resume()
func on_exit(args:Dictionary = {}):
	GameManager.translate_game_state(GameEnum.GameState.Gaming)
	super.on_exit()
func get_view_state() -> GameEnum.ViewState:
	return super.get_view_state()
#endregion
#region 公共方法
func update_slot_btns():
	_amount = _entity.get_entity_data().weapon_max_amount
	var weapon_component:WeaponComponent = _entity.get_component_by_name("WeaponComponent") as WeaponComponent
	for idx in range(8):
		var weapon:Weapon = weapon_component.get_weapon(idx)
		var slot_btn:SlotBtn = weapon_slot_btns.get_child(idx)
		slot_btn.visible = weapon != null
		if weapon:
			slot_btn.set_icon(weapon.get_weapon_sprite())
		slot_btn.icon.material.set_shader_parameter("enable",weapon_component.cur_weapon_index == idx)
func update_desc(idx:int):
	if idx == -1:
		desc.text = ""
	else:
		var weapon_component:WeaponComponent = _entity.get_component_by_name("WeaponComponent") as WeaponComponent
		var weapon:Weapon = weapon_component.get_weapon(idx)
		if weapon:
			desc.text = weapon.get_weapon_name()
func switch(idx:int):
	var weapon_component:WeaponComponent = _entity.get_component_by_name("WeaponComponent") as WeaponComponent
	weapon_component.switch_weapon(idx)
	GameUISystem.view_manager.close_view(GameEnum.ViewType.CircleBtnView)
#endregion
#region 私有方法
func _on_weapon_max_amount_changed(change_tag:String):
	if change_tag == "weapon_max_amount":
		update_slot_btns()

#endregion
#region 生命周期函数
#endregion
